import { ref } from 'vue';
import moment from 'moment';
import { ProcessTypeTree } from '@/api/smart-tools/index';
//推送类别
const typeList = ref<any>([]);
ProcessTypeTree().then(res => {
  typeList.value = res.data;
});

//流转单表格头部
export const columns = ref<any>([
  {
    title: '流转单ID',
    width: '120px',
    dataIndex: 'id',
  },
  {
    title: '流转单标题',
    width: '240px',
    dataIndex: 'title',
  },
  {
    title: '推送类别',
    width: '240px',
    dataIndex: 'type',
    customRender: ({ record }) => {
      return record.typeParentName + '-' + record.typeName;
    },
  },
  {
    title: '下发时间',
    width: '120px',
    dataIndex: 'createTime',
  },
  {
    title: '处理期限',
    width: '100px',
    dataIndex: 'handleDay',
    customRender: ({ text }) => {
      return text + '天';
    },
  },
  {
    title: '超时状态',
    width: '120px',
    slots: { customRender: 'overtimeState' },
  },
  {
    title: '接收人员',
    width: '150px',
    slots: { customRender: 'receive' },
  },
  {
    title: '流转状态',
    width: '440px',
    slots: { customRender: 'state' },
  },
  {
    title: '操作',
    align: 'center',
    fixed: 'right',
    width: '260px',
    slots: { customRender: 'action' },
  },
]);

//流转单统计表格头部
export const columnsStatistics = ref<any>([
  {
    title: '民警',
    dataIndex: 'handlePerson',
  },
  {
    title: '所属部门',
    dataIndex: 'xqdwName',
  },
  {
    title: '已下发',
    dataIndex: 'approveSum',
  },
  {
    title: '已完成',
    dataIndex: 'finishSum',
  },
  {
    title: '签收平均时长',
    dataIndex: 'time',
    customRender: ({ text }) => {
      return text + '天';
    },
  },
  {
    title: '超时反馈',
    dataIndex: 'overtimeSum',
  },
  {
    title: '未反馈',
    dataIndex: 'receivedSum',
  },
]);

//倒计时
export const countDown = (handleEndTime, finishTime) => {
  if (finishTime) {
    //完成时间-处理截止时间
    const date1 = moment(finishTime); //完成时间
    const date2 = moment(handleEndTime); //截止时间
    const date3 = date1.diff(date2, 'minute') / 60;
    if (date3 > 0) {
      const time1 = ref<any>(moment(handleEndTime).format('X'));
      const time2 = ref<any>(moment(finishTime).format('X'));
      const timeVal = time2.value - time1.value;
      // 时间戳转换
      const d = Math.floor(timeVal / (24 * 3600));
      const h = Math.floor((timeVal - 24 * 3600 * d) / 3600);
      const m = Math.floor((timeVal - 24 * 3600 * d - h * 3600) / 60);
      const s = Math.floor(timeVal - 24 * 3600 * d - h * 3600 - m * 60);
      //  当时分秒小于10的时候补0
      const hh = h < 10 ? '0' + h : h;
      const mm = m < 10 ? '0' + m : m;
      const ss = s < 10 ? '0' + s : s;
      let time;
      if (d >= 1) {
        time = ' ' + d + '天以上';
      } else {
        time = ' ' + hh + '时' + mm + '分' + ss + '秒';
      }
      return { vueTime: time, status: '已超时' };
    } else {
      return { vueTime: '', status: '未超时' };
    }
  } else {
    const date1 = moment(handleEndTime);
    const date2 = moment();
    const date3 = date1.diff(date2, 'minute') / 60;
    if (date3 <= 24 && date3 >= 0) {
      const time1 = ref<any>(moment(handleEndTime).format('X'));
      const time2 = ref<any>(moment().format('X'));
      const timeVal = time1.value - time2.value;
      // 时间戳转换
      const d = Math.floor(timeVal / (24 * 3600));
      const h = Math.floor((timeVal - 24 * 3600 * d) / 3600);
      const m = Math.floor((timeVal - 24 * 3600 * d - h * 3600) / 60);
      const s = Math.floor(timeVal - 24 * 3600 * d - h * 3600 - m * 60);
      //  当时分秒小于10的时候补0
      const hh = h < 10 ? '0' + h : h;
      const mm = m < 10 ? '0' + m : m;
      const ss = s < 10 ? '0' + s : s;
      const time = ' ' + hh + '时' + mm + '分' + ss + '秒';
      return { vueTime: time, status: '倒计时' };
    } else if (date3 < 0) {
      const time1 = ref<any>(moment(handleEndTime).format('X'));
      const time2 = ref<any>(moment().format('X'));
      const timeVal = time2.value - time1.value;
      // 时间戳转换
      const d = Math.floor(timeVal / (24 * 3600));
      const h = Math.floor((timeVal - 24 * 3600 * d) / 3600);
      const m = Math.floor((timeVal - 24 * 3600 * d - h * 3600) / 60);
      const s = Math.floor(timeVal - 24 * 3600 * d - h * 3600 - m * 60);
      //  当时分秒小于10的时候补0
      const hh = h < 10 ? '0' + h : h;
      const mm = m < 10 ? '0' + m : m;
      const ss = s < 10 ? '0' + s : s;
      let time;
      if (d >= 1) {
        time = ' ' + d + '天以上';
      } else {
        time = ' ' + hh + '时' + mm + '分' + ss + '秒';
      }
      return { vueTime: time, status: '已超时' };
    } else if (date3 > 24) {
      return { vueTime: '', status: '未超时' };
    }
  }

  //   const date1 = moment(time);
  //   const date2 = moment();
  //   const date3 = date1.diff(date2, 'minute') / 60;
  //   let vueTimedjs = '';
  //   let vueTimecs = '';
  //   if (date3 <= 24 && date3 >= 0) {
  //     const time1 = ref<any>(moment(time).format('X'));
  //     setInterval(() => {
  //       const time2 = ref<any>(moment().format('X'));
  //       const timeVal = time1.value - time2.value;
  //       // 时间戳转换
  //       const d = Math.floor(timeVal / (24 * 3600));
  //       const h = Math.floor((timeVal - 24 * 3600 * d) / 3600);
  //       const m = Math.floor((timeVal - 24 * 3600 * d - h * 3600) / 60);
  //       const s = Math.floor(timeVal - 24 * 3600 * d - h * 3600 - m * 60);
  //       //  当时分秒小于10的时候补0
  //       const hh = h < 10 ? '0' + h : h;
  //       const mm = m < 10 ? '0' + m : m;
  //       const ss = s < 10 ? '0' + s : s;
  //       vueTimedjs = ' ' + hh + '时' + mm + '分' + ss + '秒';
  //     }, 1000);
  //     return {
  //       vueTime: vueTimedjs,
  //       status: '倒计时',
  //     };
  //   } else if (date3 < 0) {
  //     const time1 = ref<any>(moment(time).format('X'));
  //     setInterval(() => {
  //       const time2 = ref<any>(moment().format('X'));
  //       const timeVal = time2.value - time1.value;
  //       // 时间戳转换
  //       const d = Math.floor(timeVal / (24 * 3600));
  //       const h = Math.floor((timeVal - 24 * 3600 * d) / 3600);
  //       const m = Math.floor((timeVal - 24 * 3600 * d - h * 3600) / 60);
  //       const s = Math.floor(timeVal - 24 * 3600 * d - h * 3600 - m * 60);
  //       //  当时分秒小于10的时候补0
  //       const hh = h < 10 ? '0' + h : h;
  //       const mm = m < 10 ? '0' + m : m;
  //       const ss = s < 10 ? '0' + s : s;
  //       if (d >= 1) {
  //         vueTimecs = ' ' + d + '天以上';
  //       } else {
  //         vueTimecs = ' ' + hh + '时' + mm + '分' + ss + '秒';
  //       }
  //     }, 1000);
  //     return {
  //       vueTime: vueTimecs,
  //       status: '已超时',
  //     };
  //   } else if (date3 > 24) {
  //     return {
  //       vueTime: '',
  //       status: '未超时',
  //     };
  //   }
};
